#pragma once
#include <iostream>

template <class T>
class MyArray {

	int maxSize;
	int currSize;
	T* pArray;

	void resize(){
		T *temp = new T[maxSize*2];		
		for(int i =0; i<maxSize; i++) {
			temp[i] = pArray[i];
		}
		delete[] pArray;
		maxSize = maxSize*2;
		pArray = temp;
	};
	
	void moveLeft(int index)
	{
		for(int i = index; i<currSize-1;i++) {
			pArray[i] = pArray[i+1];
		}
	};

	void moveRight(int index)
	{
		for(int i = currSize; i > 0 ; i--) {
			pArray[i] = pArray[i-1];
		}
	};
public:

	MyArray::MyArray()
	{
		maxSize = 10;
		currSize = 0;
		pArray = new T[10];
	};

	MyArray::~MyArray()
	{
		if(pArray != 0)
		{
			delete[] pArray;
		}

		pArray = 0;
		currSize = 0;
	};

	bool add(T object){
		if(currSize == maxSize)
		{
			resize();
		}
		pArray[currSize] = object;
		currSize++;
		return true;
	};

	bool add(int index, T object){
		if(index<currSize && index > -1)
		{
			if(currSize == maxSize) {
				resize();
			}
			moveRight(index);
			pArray[index] = object;
			currSize++;
			return true;
		}
		return false;
	};

	bool clear(){
		delete[] pArray;
		pArray = new T[10];
		currSize = 0;
		maxSize = 10;
		return true;
	};

	bool contains(T object){
		for(int i = 0;i <currSize;i++) {
			if(pArray[i]==object) {
				return true;
			}
		}
		return false;
	};

	T get(int index){
		if(index < currSize && index > -1)
		{
			return pArray[index];
		}
		return NULL;
	};

	int indexOf(T object) {
		for(int i=0;i<currSize;i++) {
			if(pArray[i] == object) {
				return i;
			}
		}	
		return -1;
	};

	bool isEmpty() {
		if(currSize == 0) {
			return true;
		}
		return false;
	};

	T remove(int index){
		if(index < currSize && index > -1)
		{
			T temp = pArray[index];
			moveLeft(index);
			currSize--;
			return temp;
		}
		return NULL;
	};

	T remove(T object){
		for(int i=0; i< currSize; i++) {
			if(pArray[i] == object) {
				T temp = pArray[i];
				moveLeft(i);
				currSize--;
				return temp;
			}
		}
		return NULL;
	};

	T set(int index, T object){
		if(index < currSize && index > -1)
		{
			T temp = pArray[index];
			pArray[index] = object;
			return temp;
		}
		return NULL;
	};

	int size(){
		return currSize;
	};

	void dump(){
		for(int i = 0; i < currSize; i++){
			std::cout << "[" << i << "] = " << pArray[i] << std::endl;
		}
	};
};

